<!--移动签批->公文详情->收文->批示分送-->
<template>
  <view>
		<van-popup v-model="showPsfs" closeable round position="bottom" :style="{ height: '100%' }"
				@click-close-icon="toClose" @click-overlay="toClose">
			<van-row type="flex" justify="center">
				<van-col offset="3" span="8">
					<van-cell value="批示分送" />
				</van-col>
			</van-row>
			<view class="padding-bottom">
				<van-cell icon="newspaper-o" title="已分送单位" title-class="text-bold"/>
				<view>
					<van-row justify="center">
						<van-col span="6">
							<van-cell title="接收单位" title-class="text-bold"></van-cell>
						</van-col>
						<van-col span="6">
							<van-cell title="当前环节" title-class="text-bold"></van-cell>
						</van-col>
						<van-col span="6">
							<van-cell title="当前状态" title-class="text-bold"></van-cell>
						</van-col>
						<van-col span="6">
							<van-cell title="操作" title-class="text-bold"></van-cell>
						</van-col>
					</van-row>
					<van-row justify="center" v-for="item in psfsList">
						<template>
							<van-col span="6">
								<van-cell :title="item.deptName"></van-cell>
							</van-col>
							<van-col span="6">
								<van-cell :title="item.userName"></van-cell>
							</van-col>
							<van-col span="6">
								<van-cell :title="item.runType"></van-cell>
							</van-col>
							<van-col span="6">
								<van-cell title="恢复" v-if="item.state == 'closed.completed'" @click='recovery(item.subproInstId)'
													title-class="text-blue"></van-cell>
								<van-cell title="撤回" v-else @click='withdraw(item.subproInstId, item.deptId)'
													title-class="text-blue"></van-cell>
							</van-col>
						</template>
					</van-row>
				</view>
			</view>
			<view class="padding-bottom">
				<van-cell icon="sort" title="批示分送单位选择" title-class="text-bold"/>
				<van-grid :column-num="3" :border="false" :center="false" >
					<van-checkbox-group v-model="psfsResult" direction="horizontal" >
						<van-grid-item v-for="(item,index) in deptList">
							<van-checkbox  :name="item.deptId" icon-size="34rpx" :key="item.deptId"
								:disabled='psfsDeptIds.filter(t => t == item.deptId).length > 0'>
								{{item.deptName}}
							</van-checkbox>
						</van-grid-item>
					</van-checkbox-group>
				</van-grid>
			</view>
			<view class="padding-top-lg padding-bottom-lg">
				<van-row type="flex" justify="space-around">
					<van-col span="10">
						<van-button type="info" round plain hairline block @click="toClose">取消</van-button>
					</van-col>
					<van-col span="10">
						<van-button type="info" round block @click="toSave">分送</van-button>
					</van-col>
				</van-row>
			</view>
		</van-popup>
	</view>
</template>

<script>

export default {
  props: {
    showDistribute: {
      type: Boolean,
      default: false
    },
    paramObject: {
      type: Object
    },
		detailObject: {
      type: Object
    }
  },

  data() {
		return {
			showPsfs: false,
			proInstId: '', // openwfneededdata接口返回的proInstId
			configId: '', // openwfneededdata接口返回的configId
			proDirId: '', // openwfneededdata接口返回的proDirId
			actInstId: '', // openwfneededdata接口返回的actInstId
			actDefId: '', // openwfneededdata接口返回的actDefId
			swId: '', // 公文详情中的swId
			title: '', // 公文详情中的title
			rootProInstId: '', // 公文详情中的proInstId
			psfsDeptIds: [], // 之前已分送的单位id
			psfsResult: [], // 当前选择的分送单位id
			psfsList: [], // 已分送单位集合
			deptList: [] // 分送单位集合
		};
  },

	watch: {
		showDistribute(val) {
			this.showPsfs = val
      this.psfsResult = []
		},
		paramObject: {
			handler(nval, oval) {
				if (nval != null && JSON.stringify(nval) != "{}") {
					console.log('---------------paramObject.proInstId:' + nval.proInstId);
					this.proInstId = nval.proInstId
					this.configId = nval.configId
					this.proDirId = nval.proDirId
					this.actInstId = nval.actInstId
					this.actDefId = nval.actDefId
					this.initDept(nval.proInstId)
				}
			},
			immediate: true,
			deep: true
		},
		detailObject: {
			handler(nval, oval) {
				if (nval != null && JSON.stringify(nval) != "{}") {
					console.log('---------------detailObject.proInstId:' + nval.proInstId);
					this.swId = nval.swId
					this.title = nval.title
					this.rootProInstId = nval.proInstId
					// 获取已分送单位
					this.initAlreadyFs(nval.proInstId)
				}
			},
			immediate: true,
			deep: true
		}
	},

  methods: {
		// 获取已分送单位
		initAlreadyFs(proInstId) {
			this.$http({
				url: this.$http.adornUrl(`/caac/oa/component/psfs/listpsfs`),
				method: 'get',
				params: this.$http.adornParams({
					'systemKey': this.cache.get('userInfo').systemKey,//固定值
					'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
					'proInstId': proInstId
				})
			}).then(({data}) => {
				if(data.code==200){
					this.psfsList = data.data.data
          this.psfsDeptIds = []
					// 回显分送单位
					this.psfsList.forEach((p, index) => this.psfsDeptIds.push(p.deptId))
				}
			})
		},

		// 初始化批示分送单位
		initDept(proInstId) {
			this.$http({
				url: this.$http.adornUrl(`/caac/oa/component/psfs/listdepts`),
				method: 'get',
				params: this.$http.adornParams({
					'systemKey': this.cache.get('userInfo').systemKey,//固定值
					'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
					'proInstId': proInstId
				})
			}).then(({data}) => {
				if(data.code==200){
					this.deptList = data.data.dept
				}
			})
		},

		// 取消按钮
		toClose(){
			this.$emit('closeDistribute', this.showPsfs)
		},

		// 确定按钮
		toSave(){
			if(this.psfsResult.length > 0){
				this.distributeDept(this.psfsResult.toString())
			}else{
			  this.$toast('未选中任何单位')
			}
		},

		// 分送
		distributeDept(deptIds) {
			let _this = this
			this.$httploading({
				url: this.$httploading.adornUrl(`/caac/oa/component/psfs/dopsfs`),
				method: 'post',
				params: this.$httploading.adornParams({
					'systemKey': this.cache.get('userInfo').systemKey,  // 固定值
					'systemFlag': this.cache.get('userInfo').systemFlag // 固定值
				}),
				data: JSON.stringify({
					"proInstId": _this.proInstId,
					"deptIds": deptIds,
					"title": _this.title,
					"configId": 'glj_psfs_zlc',
					"rootProInstId": _this.rootProInstId
				})
			}).then(({data}) => {
				if (data.code == 200) {
          _this.updateForm()
          _this.$toast('分送成功')
				} else {
				  this.$toast(data.message + ' code:' + data.code)
				}
			})
		},

		// 撤回
		withdraw(subProInstId, deptId) {
			this.$httploading({
				url: this.$httploading.adornUrl(`/caac/oa/component/psfs/remove`),
				method: 'post',
				params: this.$httploading.adornParams({
					'systemKey': this.cache.get('userInfo').systemKey,   // 固定值
					'systemFlag': this.cache.get('userInfo').systemFlag, // 固定值
					'data': JSON.stringify({
						"configId": this.configId,
						"subProInstId": subProInstId,
						"proInstId": this.rootProInstId,
						"proDirId": this.proDirId,
						"actInstId": this.actInstId
					})
				}),
			}).then(({data}) => {
				if (data.code == 200) {
					// 撤回后，取消禁止选中
					this.psfsDeptIds.splice(this.psfsDeptIds.indexOf(deptId), 1)
					// 撤回后，将该单位从已分送列表中移除
					this.psfsList = this.psfsList.filter( t => t.subproInstId != subProInstId)
          this.updateForm()
				  this.$toast('撤回成功')
				} else {
				  this.$toast(data.message + ' code:' + data.code)
				}
			})
		},

		// 恢复
		recovery(subProInstId) {
			this.$httploading({
				url: this.$httploading.adornUrl(`/caac/wfm/ApplicationController/reviveProcess`),
				method: 'post',
				params: this.$httploading.adornParams({
					'systemKey': this.cache.get('userInfo').systemKey,   // 固定值
					'systemFlag': this.cache.get('userInfo').systemFlag, // 固定值
					'data': JSON.stringify({
						"wfmData": {
							"configId": this.configId,
							"proInstId": subProInstId,
							"proDirId": this.proDirId,
							"actDefId": this.actDefId
						}
					})
				}),
			}).then(({data}) => {
				if (data.code == 200) {
					let state = data.data.wfmData.workitems[0].preState
					let deptData = this.psfsList.filter( t => t.subproInstId == subProInstId)[0]
					this.$set(deptData, 'state', state)
					this.$set(deptData, 'runType', '流转中')
				  this.$toast('恢复成功')
				} else {
				  this.$toast(data.message + ' code:' + data.code)
				}
			})
		},

    // 更新表单
    updateForm() {
		  let _this = this
      _this.$httploading({
        url: _this.$httploading.adornUrl(`/caac/oa/doc/hbglj/sd/psfsdept`),
        method: 'post',
        params: _this.$httploading.adornParams({
          'systemKey': _this.cache.get('userInfo').systemKey,  // 固定值
          'systemFlag': _this.cache.get('userInfo').systemFlag // 固定值
        }),
        data: JSON.stringify({
          "proInstId": _this.rootProInstId
        })
      }).then(({data}) => {
        if (data.code == 200) {
          _this.$emit('closeDistribute', _this.showPsfs)
          _this.$emit('refreshBtnGroup', true) // 通知父组件重新加载数据

        } else {
          _this.$toast(data.message + ' code:' + data.code)
        }
      })
    }

	},

}
</script>

<style>
	.action{
		width: auto !important;
		max-width: 150rpx !important;
	}

	.van-checkbox--horizontal {
		margin-right: 2px;
	}

	/deep/ .van-grid-item__content {
	    display: -webkit-box;
	    display: -webkit-flex;
	    display: flex;
	    -webkit-box-orient: vertical;
	    -webkit-box-direction: normal;
	    -webkit-flex-direction: column;
	    flex-direction: column;
	    box-sizing: border-box;
	    height: 100%;
	    padding: 10px 8px;
	    background-color: #fff;
	}
</style>
